#include "StdAfx.h"
#include "UserCollection.h"


UserCollection::UserCollection(void)
{
	userIdSeq = 0;
}


UserCollection::~UserCollection(void)
{
	for(vector<User*>::iterator it = users.begin(); it != users.end(); ++it)
		delete *it;
}

bool UserCollection::hasUser(const string& name)
{
	if( userDict.find(name)!=userDict.end() )
		return true;
	else
		return false;
}//modify


/*check the username and the passname */
bool UserCollection::isValid(const string& name, const string& password)
{
	User user = getUserByName(name);

	if( user.getPassword() != password )
		return false;
	else
		return true;
}//modify


void UserCollection::addUser(const string& name, const string& password)
{
	User* add = new User(userIdSeq, name, password);
	users.push_back(add);
	userDict.insert( map<string,int>::value_type(name,userIdSeq));
	userIdSeq++;		//update the users number
}//modify

int UserCollection::getIdByName(const string& name)
{
	return userDict.find(name)->second;
}

User& UserCollection::getUserByName(const string& name)
{
	int id = getIdByName(name);
	return *users[id];			//users is a vector of *User
}//modify

const User& UserCollection::getUserById( int id ) const
{
	return *users[id];
}

const int UserCollection::getUserNum() const
{
	return userIdSeq;
}




